import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);
// 0. 如果使用模块化机制编程，导入Vue和VueRouter，要调用 Vue.use(VueRouter)

// 2. 定义路由
// 每个路由应该映射一个组件。 其中"component" 可以是
// 通过 Vue.extend() 创建的组件构造器，
// 或者，只是一个组件配置对象。
// 我们晚点再讨论嵌套路由。
const routes = [
  {
    path: "/",
    component: () => import("../components/Home.vue"),
    meta: { title: "自述文件" },
  },
  {
    path: "/home",
    name: "home",
    component: () => import("../components/page/HomePage.vue"),
    meta: { title: "首页", requireAuth: true },
  },
  {
    path: "/v2/home",
    name: "homev2",
    component: () => import("../components/page/HomePagev2.vue"),
    meta: { title: "首页", requireAuth: true },
  },
  {
    path: "/v3/home",
    name: "homev3",
    component: () => import("../components/page/HomePagev3.vue"),
    meta: { title: "首页", requireAuth: true },
  },
  {
    path: "/timelineAnalysis",
    name: "timelineAnalysis",
    component: () => import("../components/page/AttendanceAnalysis.vue"),
    meta: { title: "考勤时间轴分析", requireAuth: true },
  },
];

let router = new Router({
  mode: "hash",
  base: "/",
  routes: routes,
});
const routerPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return routerPush.call(this, location).catch((error) => error);
};

// https://router.vuejs.org/zh/guide/advanced/meta.html
router.beforeEach((to, from, next) => {
  console.log(to, from, next);
  next(); // 确保一定要调用 next()
});
export default router;
